#!/usr/bin/env python
# coding: utf-8
import os
import time

import win32con
import win32gui
from PyQt5.QtGui import QIcon
from cfnUI import Ui_MainWindow
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *

import tkinter as tk
from tkinter import filedialog
import os

currentPath = ''
currentFile = ''
dontInLis = [ ' \\', '/ ',':',' * ','?',' "',' < ','>',' |' ,"'" ]
unfoldValue = True
class CFN(QMainWindow):
    def __init__(self):
        super(CFN, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.initUI()
        self.setWindowTitle('CFN by大山猛')
        self.setWindowIcon(QIcon('ui_img/icon.jpg'))
        self.setFixedSize(self.width(), self.height())



    def initUI(self):

        #连接方法
        self.ui.sureRemberCBt.setChecked(False)
        self.ui.openDirBt.clicked.connect(self.openDirBt)
        self.ui.changeNameBt.clicked.connect(self.changeNameBt)
        self.ui.unfoldBt.clicked.connect(self.unfoldBt)

        RadioBtG = QButtonGroup()
        self.ui.oneRBt.setChecked(True)
        RadioBtG.addButton(self.ui.oneRBt)
        RadioBtG.addButton(self.ui.twoRBt)




        self.ui.unfoldBt.setStatusTip('一次打开一层 多点几次 （属于bug 暂不解决）')
        self.ui.openDirBt.setStatusTip('打开目录')
        self.ui.changeNameBt.setStatusTip('点击修改名称')
        self.ui.oneEdit.setStatusTip('请不要输入 \\  / , :  * ?   <  > | " ')
        self.ui.twoEdit_1.setStatusTip('请不要输入 \\  / , :  * ?   <  > | " ')
        self.ui.twoEdit_2.setStatusTip('请不要输入 \\  / , :  * ?   <  > | " ')
        self.ui.sureRemberCBt.setStatusTip('是否清除内容')
        self.ui.sureRemberCBtTwo1.setStatusTip('是否1框清除内容')
        self.ui.sureRemberCBtTwo2.setStatusTip('是否2框清除内容')
        self.ui.sureRemberCBtTwo3.setStatusTip('是否3框清除内容')




    def openDirBt(self):
        #打开文件选择器
        root = tk.Tk()
        root.withdraw()
        Folderpath = filedialog.askdirectory()
        self.ui.nowDirLab.setText(Folderpath)
        print('Folderpath:',Folderpath)

        #初始化treelist
        self.mod = QFileSystemModel(self)
        self.mod.setRootPath(Folderpath)
        self.ui.treeView.setModel(self.mod)
        self.ui.treeView.setRootIndex(self.mod.index(Folderpath))
        # self.ui.treeView.expandAll()
        self.ui.treeView.selectionModel().currentChanged.connect(self.onCurrentChanged)

        for i in (1,2,3):
            self.ui.treeView.setColumnHidden(i,True)
        self.ui.treeView.setItemsExpandable(True)

    def onCurrentChanged(self,current,_):
        global currentPath,currentFile
        currentFile = current.data()
        currentPath = self.mod.filePath(current)

        self.ui.checkedItemLab.setText(currentFile)
        # print(self.mod.filePath(current))



    def changeNameBt(self):
        global currentPath,currentFile,dontInLis,newFileName
        if self.ui.oneRBt.isChecked():
            newFileName = self.ui.oneEdit.text()
        if self.ui.twoRBt.isChecked():
            newFileName = self.ui.twoEdit_1.text() + self.ui.twoEdit_2.text() + self.ui.twoEdit_3.text()

        oldFileName = currentFile
        oldFilePath = currentPath


        if newFileName == '' or newFileName == None:
            self.ui.logLab.setText("改名失败 内容为空")
            return

        if newFileName in dontInLis :
            #todo 判断并不生效
            self.ui.logLab.setText("改名失败 包含" + str(dontInLis )+ "字符")
            return

        newFilePath = oldFilePath[0:-len(oldFileName)] + newFileName
        print(newFilePath)
        self.ui.logLab.setText("改名成功 新名字：" + newFileName)
        try:
           os.rename(oldFilePath,newFilePath)
        except :
          pass
        if self.ui.sureRemberCBt.isChecked():
            self.ui.oneEdit.setText("")
            self.ui.oneEdit.setFocus()

        if self.ui.sureRemberCBtTwo1.isChecked():
            self.ui.twoEdit_1.setText("")
            self.ui.twoEdit_1.setFocus()
        if self.ui.sureRemberCBtTwo2.isChecked():
            self.ui.twoEdit_2.setText("")
            self.ui.twoEdit_2.setFocus()
        if self.ui.sureRemberCBtTwo3.isChecked():
            self.ui.twoEdit_3.setText("")
            self.ui.twoEdit_3.setFocus()
        #self.ui.treeView.setCurrentIndex()
        #todo 返回当前修改的对象

    def unfoldBt(self):
        global unfoldValue

        if unfoldValue:
            self.ui.treeView.expandAll()
            unfoldValue = False
            self.ui.unfoldBt.setText("收缩全部")
        else:
            self.ui.treeView.collapseAll()
            unfoldValue = True
            self.ui.unfoldBt.setText("展开全部")

if __name__ == '__main__':
    app = QApplication(sys.argv)
    c = CFN()
    c.show()
    sys.exit(app.exec_())



